package hos.demo

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import hos.demo.databinding.ActivityLifecyleHandlerBinding

/**
 * <p>Title: LifecycleHandlerActivity </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2023/8/3 14:14
 * @version : 1.0
 */
class LifecycleHandlerActivity : AppCompatActivity() {

    private lateinit var binding: ActivityLifecyleHandlerBinding

    private val mViewModel by viewModels<MainViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLifecyleHandlerBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.buttonStart.setOnClickListener {
            // 开始加载进度
            mViewModel.progress.start()
            mViewModel.progressLive.start()
        }
        // 进度监听回调
        mViewModel.progress
            .onProgress {
                Log.d("Thread", "onProgress:  $this")
                Log.d("Thread", "onProgress:  $binding")
                binding.progress.progress = this.toInt()
            }.onCatch {
                Log.d("Thread", "onCatch: ")
                this.printStackTrace()
            }.onSuccess {
                Log.d("Thread", "onSuccess: ")
            }
        mViewModel.progressLive
            .onProgress {
                Log.d("Thread", "onProgress:Live  $this")
                Log.d("Thread", "onProgress:Live  $binding")
                binding.progressLive.progress = this.toInt()
            }.onCatch {
                Log.d("Thread", "onCatch:Live ")
                this.printStackTrace()
            }.onSuccess {
                progressUpdate(100.0)
                Log.d("Thread", "onSuccess:Live ")
            }
    }

    override fun onDestroy() {
        super.onDestroy()
        mViewModel.progress.onDestroy()
        mViewModel.progressLive.onDestroy()
    }
}